package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: SetmealServiceImpl
 * @Author 贾金博
 * @Package com.sky.service.impl
 * @Date 2024/3/26 16:10
 * @description:
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    /**
     * 插入套餐
     * @param setmealDTO
     */
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO>  page = setmealMapper.myPage(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }


    /**
     * 套餐批量删除
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for(Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE); // 起售中的套餐不可以删除
            }
        }
        setmealMapper.deleteBatch(ids);
        //还要删除菜品表里面的数据
        setmealDishMapper.deleteBatch(ids);
    }


    /**
     * 更改套餐的状态
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        if(status == StatusConstant.ENABLE) { // 想要把菜品变为售卖的 那 看看他这里面的菜 是不是售卖的
            List<SetmealDish> setmealDishs= setmealDishMapper.getDishesById(id);
            if(setmealDishs != null && setmealDishs.size() > 0)
            for(SetmealDish setmealDish : setmealDishs) {
                long dishId = setmealDish.getDishId();
                Dish dish = dishMapper.getById(dishId);
                if(dish != null)
                if(dish.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }


    /**
     * 跟新套餐
     * @param setmealDTO
     */
    @Override
    public void setmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //先删除菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishesById(setmeal.getId());
        List<Long>list = new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishes) {
            Long id = setmealDish.getId();
            list.add(id);
        }
//        log.info("list: {}", list);
        setmealDishMapper.deleteBatch(list);
        //再去插入
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insertBatch(dishes);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealDTO findById(Long id) {
        SetmealDTO setmealDTO = new SetmealDTO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDTO);

        List<SetmealDish> dishes = setmealDishMapper.getDishesById(id);

        setmealDTO.setSetmealDishes(dishes);

        return setmealDTO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }
}
